
//
//  YYYBaseViewController.swift
//  Sina12
//
//  Created by yang on 2017/9/21.
//  Copyright © 2017年 成品家（北京）网络科技有限公司. All rights reserved.
//

import UIKit

// MARK: 所有主控制器的基类控制器
class YYYBaseViewController: UIViewController {

    //访客视图字典
    var visitorDictionary: [String: String]?
    
    //自定义状态栏
    private lazy var customStatusView = UIView(frame: CGRect(x: 0, y: 0, width: kDeviceWidth, height: 20))
    //自定义导航条
    private lazy var customNavigationBar = UINavigationBar(frame: CGRect(x: 0, y: 20, width: kDeviceWidth, height: 44))
    //自定义导航条目
    lazy var customNavigationItem = UINavigationItem()
    
    //表视图
    lazy var baseTableView = UITableView(frame: CGRect(x: 0, y: 64, width: kDeviceWidth, height: kDeviceHeight-64-49), style: .plain)

    //刷新控件
    lazy var refreshControl = MeituanRefreshControl()
    
    override func viewDidLoad() {
        super.viewDidLoad()
  
        //设置界面
        setupUI()
        
        //三目实现 加载数据
        YYYNetworkManager.shared.userLoginOn ? loadData() : ()
       
        //注册监听者
        NotificationCenter.default.addObserver(self, selector: #selector(loginSuccessed), name: NSNotification.Name(rawValue: UserLoginSuccessedNotification), object: nil)
    }
    //注销通知
    deinit {
        NotificationCenter.default.removeObserver(self)
    }
    
    //加载数据
    @objc func loadData() {
        
    }
    //重写title的didSet方法
    override var title: String?
    {
        didSet
        {
            customNavigationItem.title = title
        }
    }
}

//设置界面
extension YYYBaseViewController
{
    private func setupUI()
    {
        //取消自动缩进
        //automaticallyAdjustsScrollViewInsets = false
        
        UIApplication.shared.isStatusBarHidden = false
        //添加状态栏
        UIApplication.shared.keyWindow?.addSubview(customStatusView)
        customStatusView.backgroundColor = UIColor.hexColor(hex: 0xF6F6F6)
        
        //添加自定义导航条
        view.addSubview(customNavigationBar)
        
        //设置导航条目
        customNavigationBar.items = [customNavigationItem]
       
        //设置导航栏背景的渲染颜色
        customNavigationBar.barTintColor = UIColor.hexColor(hex: 0xF6F6F6)
       
        //设置导航栏标题的字体颜色
        customNavigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.darkGray]
        
        //设置导航栏默认item的字体颜色
        customNavigationBar.tintColor = UIColor.RGBColor(red: 20, green: 82, blue: 184)
        
        //自定义表视图
        YYYNetworkManager.shared.userLoginOn ? setupTableView() : setupVisitorView()
    }
    
    //自定义表视图
    @objc func setupTableView()
    {
        view.addSubview(baseTableView)
        
        //设置数据源、代理
        baseTableView.dataSource = self
        baseTableView.delegate = self
        
        //添加刷新控件到表格视图
        baseTableView.addSubview(refreshControl)
        //添加监听方法
        refreshControl.addTarget(self, action: #selector(loadData), for: .valueChanged)
    }
    
    //自定义访客视图
    private func setupVisitorView()
    {
        //未登录状态，导航栏左侧、右侧按钮
        //customNavigationItem.leftBarButtonItem = UIBarButtonItem(title: "注册", style: .plain, target: self, action: #selector(registerAction))
        customNavigationItem.rightBarButtonItem = UIBarButtonItem(title: "登录", style: .plain, target: self, action: #selector(loginAction))
        
        let visitorView = YYYVisitorView(frame: view.bounds)
        view.insertSubview(visitorView, belowSubview: customNavigationBar)
        visitorView.visitorDictInfo = visitorDictionary
        visitorView.registerButton.addTarget(self, action: #selector(registerAction), for: .touchUpInside)
        visitorView.loginButton.addTarget(self, action: #selector(loginAction), for: .touchUpInside)
    }
}

extension YYYBaseViewController
{
    @objc private func loginSuccessed(n: Notification)
    {
        print(n)
        customNavigationItem.leftBarButtonItem = nil
        customNavigationItem.rightBarButtonItem = nil
        //当view=nil，会重新执行loadView->viewDidload
        view = nil
        //注销通知，避免重复注册
        NotificationCenter.default.removeObserver(self)
    }
    //添加导航栏左侧、右侧按钮事件
    @objc private func registerAction()
    {
        print("注册")
    }
    @objc private func loginAction()
    {
        print("登录")
        //发送通知
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: UserShouldLoginNotification), object: nil)
    }
}

// MARK: UITableViewDataSource,UITableViewDelegate
extension YYYBaseViewController: UITableViewDataSource,UITableViewDelegate
{
    //基类负责方法的准备，子类负责具体方法的实现
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        return UITableViewCell()
    }
}
